﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using MacomberMapSystem.Common.Serialization;

namespace MacomberMapSystem.Common.Internals
{
    /// <summary>
    /// This class handles conversions between different types
    /// </summary>
    public static class MM_Converter
    {
        /// <summary>
        /// Retrieve a single-precision number, first checking for NaN
        /// </summary>
        /// <param name="Incoming">The incoming value</param>
        /// <returns></returns>
        public static Single ToSingle(Object Incoming)
        {
            if (Incoming is DBNull)
                return float.NaN;
            else if (Incoming is Single)
                return (Single)Incoming;
            else if (Incoming is Double)
                return Convert.ToSingle(Incoming);
            else if (Incoming is Decimal)
                return Convert.ToSingle(Incoming);
            else if ((String)Incoming == "NaN")
                return float.NaN;
            else if (String.IsNullOrEmpty((String)Incoming))
                return float.NaN;
            else
                return Convert.ToSingle((String)Incoming);
        }

        
        /// <summary>
        /// Determine the center of a rectangle
        /// </summary>
        /// <param name="Rect">The incoming rectangle</param>
        /// <returns></returns>
        public static Point CenterRect(Rectangle Rect)
        {
            return new Point(Rect.Left + (Rect.Width / 2), Rect.Top + (Rect.Height / 2));
        }
    }
}
